<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>cesium|多线程的实现 | 一只咸鱼 | 努力学习最新姿势！！！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Cesium">
    <meta name="description" content="Web Worker什么是 web worker js本身是一种单线程设计，我们无法在同一时刻并行运行多个脚本。虽然可以用setInterval，setTimeout方法来模拟多线程，但实际上这些方法都是存在于主线程使用的一个事件循环里，一旦存在一个耗时操作，就会牵制主线程的操作，导致页面卡顿。 Web Worker通过引入类似线程的机制使这种问题得到了解决，通过在当前js主线程中使用worker">
<meta property="og:type" content="article">
<meta property="og:title" content="cesium|多线程的实现">
<meta property="og:url" content="http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="一只咸鱼">
<meta property="og:description" content="Web Worker什么是 web worker js本身是一种单线程设计，我们无法在同一时刻并行运行多个脚本。虽然可以用setInterval，setTimeout方法来模拟多线程，但实际上这些方法都是存在于主线程使用的一个事件循环里，一旦存在一个耗时操作，就会牵制主线程的操作，导致页面卡顿。 Web Worker通过引入类似线程的机制使这种问题得到了解决，通过在当前js主线程中使用worker">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-11T13:48:00.000Z">
<meta property="article:modified_time" content="2020-04-12T07:18:43.020Z">
<meta property="article:author" content="Mr.Zhang">
<meta property="article:tag" content="Cesium">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="一只咸鱼" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Mr.Zhang</h5>
          <a href="mailto:zhangticcc@163.com" title="zhangticcc@163.com" class="mail">zhangticcc@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                首页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                时间轴
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5392412243" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                微博
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhangti0708" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                开源地址
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://blog.csdn.net/weixin_40902527" target="_blank" >
                <i class="icon icon-lg icon-rss"></i>
                技术博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://zhangticcc.gitee.io/webgis/" target="_blank" >
                <i class="icon icon-lg icon-globe"></i>
                GIS案例
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://mp.weixin.qq.com/s/p4TOhKcr4RZkZWzuMYyBEQ" target="_blank" >
                <i class="icon icon-lg icon-weixin"></i>
                微信订阅号
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">cesium|多线程的实现</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">cesium|多线程的实现</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-04-11T13:48:00.000Z" itemprop="datePublished" class="page-time">
  2020-04-11
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Web-Worker"><span class="post-toc-number">1.</span> <span class="post-toc-text">Web Worker</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#cesium实现的多线程"><span class="post-toc-number">2.</span> <span class="post-toc-text">cesium实现的多线程</span></a></li></ol>
        </nav>
    </aside>


<article id="post-cesium-多线程的实现"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">cesium|多线程的实现</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-04-11 21:48:00" datetime="2020-04-11T13:48:00.000Z"  itemprop="datePublished">2020-04-11</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h3 id="Web-Worker"><a href="#Web-Worker" class="headerlink" title="Web Worker"></a>Web Worker</h3><p>什么是 web worker</p>
<p><code>js本身是一种单线程设计，我们无法在同一时刻并行运行多个脚本。虽然可以用setInterval，setTimeout方法来模拟多线程，但实际上这些方法都是存在于主线程使用的一个事件循环里，一旦存在一个耗时操作，就会牵制主线程的操作，导致页面卡顿。</code></p>
<p><code>Web Worker通过引入类似线程的机制使这种问题得到了解决，通过在当前js主线程中使用worker类加载一个js文件来开辟一个新线程，起到互不阻塞执行的效果。这个js worker运行在另一个全局上下文中，不同于当前的window，所以不能用window变量来获取当前全局的范围。</code></p>
<p>什么时候使用</p>
<p><code>1 当需要一个js脚本进行大量的复杂计算时候，通过postMessage和onmessage进行通信
2 功能模块化。importScripts这个方法只能在worker线程中执行，该方法可以在worker线程中引入多个脚本，则该线程可以使用所引入脚本中的任意变量和函数。
3 当需要执行一个不断向后台发送更新请求的时候，可以将这个过程放到工作线程里，然后将结果返回给主线程。</code></p>
<p>如何使用</p>
<p>①创建一个线程：<br>通过new一个Worker实例来创建一个线程，构造函数参数传递一个指向js文件资源的url。<br><code>const worker = new Worker(&#39;a.js&#39;)</code></p>
<p>②与一个线程通信：<br>主线程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">onload &#x3D;function()&#123;</span><br><span class="line">    var worker &#x3D;new Worker(&#39;a.js&#39;);</span><br><span class="line">    worker.onmessage &#x3D; function (evt) &#123;&#x2F;&#x2F;接收子线程消息</span><br><span class="line">        console.log(evt.data); &#x2F;&#x2F;hello received</span><br><span class="line">    &#125;;</span><br><span class="line">    worker.postMessage(&quot;hello&quot;)&#x2F;&#x2F;向子线程发送消息</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>子线程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">onmessage &#x3D;function(event) &#123; &#x2F;&#x2F;接收主线程消息</span><br><span class="line">    let str &#x3D; event.data;</span><br><span class="line">    postMessage(str+&quot; received&quot;); &#x2F;&#x2F;发送子线程消息</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>在主线程中，消息事件依托于创建出来的worker对象，而在工作线程中，消息事件依托于全局对象。</p>
<p>当工作线程完成了任务后，需要调用terminate方法来释放内存和避免僵尸线程的情况：<br><code>worker.terminate();</code></p>
<h3 id="cesium实现的多线程"><a href="#cesium实现的多线程" class="headerlink" title="cesium实现的多线程"></a>cesium实现的多线程</h3><p>如何使用: </p>
<p><code>1 用户只需要创建一个TaskProcessor，指定具体需要创建线程的类型</code>  ：圆</p>
<p><code>2 然后调用scheduleTask，里面是该对象的具体参数</code> : 圆position + 半径</p>
<p><code>3 处理结果通过promise机制返回一个异步对象，方便用户使用</code></p>
<p>cesium设计的多线程会通过创建<code>TaskProcessor</code>指定参数给<code>scheduleTask</code>最后计算通过<code>Promise</code>机制返回异步处理。内部通过<code>require</code>关联对应的功能函数，并替换<code>onmessage</code>函数。</p>
<p>cesium的底层实现 :</p>
<p>原理篇 ： <code>https://www.cnblogs.com/fuckgiser/p/5869122.html</code></p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-04-12T07:18:43.020Z" itemprop="dateUpdated">2020-04-12 15:18:43</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" target="_blank" rel="external">http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/</a>
        
    </div>
    
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="Mr.Zhang">
            Mr.Zhang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Cesium/" rel="tag">Cesium</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&title=《cesium|多线程的实现》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&title=《cesium|多线程的实现》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《cesium|多线程的实现》 — 一只咸鱼&url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2020/04/11/Vue/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Vue|父子组件的通信</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2020/04/10/cesium-postProcessStages%E5%90%8E%E6%9C%9F%E5%A4%84%E7%90%86%E6%B7%BB%E5%8A%A0%E7%89%B9%E6%95%88/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">cesium| PostProcessStage后期处理</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Mr.Zhang &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&title=《cesium|多线程的实现》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&title=《cesium|多线程的实现》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《cesium|多线程的实现》 — 一只咸鱼&url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://yoursite.com/2020/04/11/cesium-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
